package com.cqc.listviewchoicedemo;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckedTextView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

/**
 * Created by ${cqc} on 2017/3/13.
 * HashMap
 */

public class CustomItemMultipleChoiceActivity extends AppCompatActivity {

    private static final String TAG = "复杂item多选";
    private ListView listView;
    private Button btn;
    private List<String> dataList = new ArrayList<>();
    private MyAdapter adapter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_multiple_choice);
        initData();
        findViews();
        initViews();

    }

    private void initData() {
        for (int i = 0; i < 100; i++) {
            dataList.add("item" + i);
        }
    }

    private void initViews() {
        //设置单选模式有2种方法：xml和java设置
        //        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
        adapter = new MyAdapter();
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                //                Log.d(TAG, "sb2=" + sb2.toString());
                MyViewHolder holder = (MyViewHolder) view.getTag();//注意是： view.getTag() 不是adapterView.getTag()
                holder.checkTV.toggle();//页面显示
                //如果不加这一行代码：当划出屏幕在划回来的时候，原来选中的状态没了
                adapter.selectMap.put(position, holder.checkTV.isChecked());
            }
        });

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                StringBuffer sb2 = new StringBuffer();
                for (int i = 0; i < dataList.size(); i++) {
                    if (adapter.selectMap.get(i)) {
                        sb2.append(dataList.get(i) + ",");
                    }
                }
                Log.d(TAG, "sb2=" + sb2.toString());
            }
        });
    }

    private void findViews() {
        listView = (ListView) findViewById(R.id.listView);
        btn = (Button) findViewById(R.id.btn);
    }


    private class MyAdapter extends BaseAdapter {

        private HashMap<Integer, Boolean> selectMap = new HashMap<>();

        public MyAdapter() {
            for (int i = 0; i < dataList.size(); i++) {
                selectMap.put(i, false);
            }
        }

        @Override
        public int getCount() {
            return dataList.size();
        }

        @Override
        public Object getItem(int position) {
            return dataList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup viewGroup) {
            MyViewHolder holder;
            if (convertView == null) {
                convertView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_multiple__custom_choice, viewGroup, false);
                holder = new MyViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (MyViewHolder) convertView.getTag();
            }
            holder.tv_choice.setText(dataList.get(position) + "");
            holder.checkTV.setChecked(selectMap.get(position));
            return convertView;
        }
    }

    public class MyViewHolder {
        TextView tv_choice;
        TextView tv_choice2;
        CheckedTextView checkTV;

        public MyViewHolder(View itemView) {
            checkTV = (CheckedTextView) itemView.findViewById(R.id.checkTV);
            tv_choice = (TextView) itemView.findViewById(R.id.tv_choice);
            tv_choice2 = (TextView) itemView.findViewById(R.id.tv_choice2);
        }
    }
}
